package com.github.aloxc.plugin.restplus.component;

import com.github.aloxc.plugin.restplus.listener.EnvironmentAddListener;
import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBPanel;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.util.Vector;

import static javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
import static javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
/**
 *
 * @author leero
 */
public class Env extends JBPanel implements ISettingPanel  {
    private JPanel rootPanel;
    private JPanel envPane;
    private Dialog dialog;
    private Project project;
    public JButton getVariateSaveButton() {
        return envSaveButton;
    }

    public JButton getCancelButton() {
        return envCancelButton;
    }

    private JButton envSaveButton;
    private JButton envCancelButton;
    private JTable envTable;
    private JPanel envTablePanel;
    private JPanel envTopPanel;
    private JPanel envBottomPanel;
    private JScrollPane tableScrollPanel;
    private JButton envDeleteButton;
    private JButton envAddButton;

    @Override
    protected void printComponent(Graphics g) {
        super.printComponent(g);
    }

    public Env(){
        super();
        initComponent();
    }

    private void initComponent() {
        initUi();
        initAction();
    }

    private void initUi() {
        GridLayoutManager envLayoutManager = new GridLayoutManager(1, 2);
        envLayoutManager.setHGap(1);
        envLayoutManager.setVGap(1);
        envLayoutManager.setMargin(new Insets(0,0,0,0));
        envPane = new JBPanel<>();
        envPane.setLayout(envLayoutManager);
        this.setBorder(BorderFactory.createEmptyBorder());
        this.setLayout(new GridLayoutManager(1, 1));
        initPanel();
    }

    private void initPanel() {
        String[] headers = { "Using", "Type","Variable", "Value","Delete" };
        JCheckBox checkBox = new JCheckBox();
        checkBox.setText("复选");

        Vector<Vector> cellData = new Vector(){{
            add(new Vector(){{
                add(true);
                add("");
                add("c1");
                add("d1");
                add(false);
            }});
            add(new Vector(){{
                add(true);
                add("");
                add("c2");
                add("d2");
                add(false);
            }});
            add(new Vector(){{
                add(true);
                add("");
                add("c3");
                add("d3");
                add(false);
            }});
            add(new Vector(){{
                add(false);
                add("");
                add("c4");
                add("d4");
                add(true);
            }});
        }};
        JComboBox comboBox = new JComboBox();
        comboBox.addItem("----");
        comboBox.addItem("Cookie");
        comboBox.addItem("Header");
        VariateTableModel model = new VariateTableModel(headers, cellData) ;
        envTable.setModel(model);
        envTable.setRowHeight(30);

        TableColumnModel tcm = envTable.getColumnModel();
        tcm.getColumn(0).setPreferredWidth(40);
        tcm.getColumn(0).setWidth(40);
        tcm.getColumn(0).setMaxWidth(40);

        tcm.getColumn(1).setCellEditor(new DefaultCellEditor(comboBox));
        tcm.getColumn(1).setCellRenderer(new DefaultTableCellRenderer());
        tcm.getColumn(1).setPreferredWidth(80);
        tcm.getColumn(1).setWidth(80);
        tcm.getColumn(1).setMaxWidth(80);

        tcm.getColumn(4).setPreferredWidth(40);
        tcm.getColumn(4).setWidth(40);
        tcm.getColumn(4).setMaxWidth(40);

        envPane.add(envTopPanel,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_FIXED,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        new Dimension(32,-1), new Dimension(32,-1), new Dimension(32,-1)));
        Dimension size460 = new Dimension(600, 300);
        GridLayoutManager tableLayoutManager = new GridLayoutManager(1, 1);
        tableLayoutManager.setHGap(1);
        tableLayoutManager.setVGap(1);
        tableLayoutManager.setMargin(new Insets(0,0,0,0));
        envTablePanel = new JBPanel<>();
        envTablePanel.setLayout(tableLayoutManager);

        ScrollPaneLayout tableScrollLayoutManager = new ScrollPaneLayout();
        tableScrollLayoutManager.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_AS_NEEDED);
        tableScrollLayoutManager.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_AS_NEEDED);
        tableScrollPanel.setLayout(tableScrollLayoutManager);

        tableScrollPanel.getViewport().add(envTable,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        size460, size460, size460));
        envTablePanel.add(tableScrollPanel,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        size460, size460, size460));


        GridLayoutManager topLayoutManager = new GridLayoutManager(1, 1);
        topLayoutManager.setHGap(1);
        topLayoutManager.setVGap(1);
        topLayoutManager.setMargin(new Insets(0,0,0,0));
        envTopPanel = new JBPanel<>();
        envTopPanel.setLayout(topLayoutManager);
        envTopPanel.add(envTablePanel,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        size460, size460, size460));

        GridLayoutManager bottomLayoutManager = new GridLayoutManager(1, 4);
        bottomLayoutManager.setHGap(1);
        bottomLayoutManager.setVGap(1);
        bottomLayoutManager.setMargin(new Insets(0,0,0,0));
        envBottomPanel = new JBPanel<>();
        envBottomPanel.setLayout(bottomLayoutManager);

        Dimension size20 = new Dimension(100, 20);

        envBottomPanel.add(envCancelButton,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        size20, size20, size20));
        envBottomPanel.add(envDeleteButton,
                new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        size20, size20, size20));
        envBottomPanel.add(envAddButton,
                new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        size20, size20, size20));
        envBottomPanel.add(envSaveButton,
                new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        size20, size20, size20));

        GridLayoutManager envLayoutManager = new GridLayoutManager(2, 1);
        envLayoutManager.setHGap(1);
        envLayoutManager.setVGap(1);
        envPane = new JBPanel<>();
        envPane.setLayout(envLayoutManager);

        envPane.add(envTopPanel,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        null, null, null));

        size20 = new Dimension(400,20);
        envPane.add(envBottomPanel,
                new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        size20, size20, size20));
        this.add(envPane,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        null, null, null));
    }

    private void initAction() {
        this.envCancelButton.addActionListener(e->{
            dialog.setVisible(false);
        });

        this.envAddButton.addActionListener(new EnvironmentAddListener(this.envTable));

        this.envDeleteButton.addActionListener(e->{
        });

        this.envSaveButton.addActionListener(e->{

        });
    }

    @Override
    public void setDialog(Dialog dialog) {
        this.dialog = dialog;
    }

    @Override
    public void setProject(Project project) {
        this.project = project;
    }
}
